<template>
  <div class="app-main-container">
    <!-- <div class="app-main-title">常见问题配置</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div v-if="meta.indexOf('NoviceHelpAdd') > -1" class="doumee-table-buttons">
      <el-button
        type="primary"
        @click="$router.push({ name: 'NoviceHelpEdit' })"
        >{{ translate("add") }}</el-button
      >
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('title')"
        prop="title"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('content')"
        prop="info"
        min-width="200"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('applyVenue')"
        prop="venueNames"
        min-width="200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            :content="scope.row.venueNames.join(',')"
            placement="top-start"
          >
            <span class="han1">{{ scope.row.venueNames.join(",") }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('status')"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-color="#435EBE"
            inactive-color="#999999"
            :active-value="'1'"
            :inactive-value="'0'"
            :disabled="meta.indexOf('NoviceHelpEdit') == -1"
            @change="toConfigUpdate(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('sort')"
        prop="sort"
        min-width="120"
      />
      <el-table-column
        align="center"
        :label="translate('createTime')"
        prop="createDate"
        min-width="200"
      />
      <el-table-column
        :label="translate('handle')"
        align="center"
        width="140"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('NoviceHelpEdit') > -1"
              v-preventReClick
              type="text"
              @click="
                $router.push({
                  name: 'NoviceHelpEdit',
                  query: { id: scope.row.id },
                })
              "
              >{{ translate("edit") }}</el-button
            >
            <el-button
              v-if="meta.indexOf('NoviceHelpDel') > -1"
              v-preventReClick
              type="text"
              style="color: red"
              @click="toDelete(scope.row.id)"
              >{{ translate("del") }}</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :total="totalCount"
        @current-change="getList"
      />
    </div>
  </div>
</template>

<script>
import { deleteKnowledge, listKnowledge, editKnowledge, venueList } from '@/api'
import { parseTime } from '@/utils/index'
import { queryFormConfig } from './config'
export default {
  name: 'NoviceHelpList',
  data() {
    return {
      queryFormConfig,
      cgList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: true,
      meta: [],
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
    if (this.$route.query.renovate && this.$route.query.renovate === '1') {
      this.getList()
    }
  },
  methods: {
    toDelete(id) {
      const that = this
      this.$confirm(this.translate('sportKnowDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          deleteKnowledge({ param: { id } }).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success(this.translate('delScs'))
            that.getList(1)
          })
        })
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[1].options = arr
        })
    },
    toConfigUpdate(v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      editKnowledge({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            if (obj.status === '0') {
              that.$message.success(this.translate('jyScs'))
            } else {
              that.$message.success(this.translate('qyScs'))
            }
            that.getList(1)
          } else {
            arr.forEach(e => {
              if (e.id === obj.id) {
                e.status = obj.status === '1' ? '0' : '1'
              }
            })
            that.list = arr
            that.listLoading = false
          }
        }).catch(() => {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.status = obj.status === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        })
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      param.type = '2'
      this.listLoading = true
      listKnowledge({
        pagination,
        param
      }, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
